package dao

import (
	"fmt"
	"service/app/dto"
	"service/app/mode"

	"gorm.io/gorm"
)

type PmDao struct {
	BaseDao
}

var pmDao *PmDao

func PmDaoInit() *PmDao {
	if pmDao == nil {
		pmDao = &PmDao{BaseDao: newBaseDao()}
	}
	return pmDao
}

/** 默认serch key 为 url*/
func (m *PmDao) PmEditC(id *dto.ReqUriIdDto, api *mode.Db_pm) error {
	var exiteApi mode.Db_pm
	err := m.Orm.Model(&mode.Db_pm{}).Where("id = ?", id.Id).Find(&exiteApi).Error
	if err != nil || exiteApi.ID == 0 {
		return fmt.Errorf("id:%v 未找到,或者其它错误", id.Id)
	}
	exiteApi.UpdatePm(api)
	return m.Orm.Save(&exiteApi).Error
}

/** 默认serch key 为 url*/
func (m *PmDao) Pm_delD(id dto.ReqUriIdDto) error {
	var msg interface{}
	if err := m.Orm.Model(&mode.Db_pm{}).Where("id = ?", id.Id).Delete(&msg); err != nil {
		// if err := m.Orm.Delete(&menu).Error; err != nil {
		return err.Error
	}
	return nil
}

/** 默认serch key 为 url*/
func (m *PmDao) Pmlist_pageD(paginat *dto.ReqPaginat) (*[]mode.Db_pm, int64, error) {
	var total int64
	var Pmlist []mode.Db_pm

	if paginat.Keyword != "" {
		// 先返回带有WHERE Total
		keyword := "%" + paginat.Keyword + "%"
		if paginat.Key == "" {
			paginat.Key = "url"
		}
		err := m.Orm.Model(&mode.Db_pm{}).Where(paginat.Key+" LIKE ?", keyword).Count(&total).
			Scopes(paginat.PaginatFn(m.Orm)).Find(&Pmlist).Error
		return &Pmlist, total, err
	} else {

		err := m.Orm.Model(&mode.Db_pm{}).Count(&total).
			Scopes(paginat.PaginatFn(m.Orm)).Find(&Pmlist).Error
		return &Pmlist, total, err
	}
}

func (m *PmDao) PmUrlRelRoleD(id *dto.ReqUriIdDto, pmUrlList *dto.Ipm_pmUrlList) error {
	var exitesPm []mode.Db_pm
	// 修改数据库结构,添加一个FullPath 2024年12月13日00:33:49
	// err := m.Orm.Model(&mode.Db_pm{}).Where("fullpath in ?", pmUrlList.Pm).Find(&exitesPm).Error
	// if err != nil {
	// 	return nil
	// }
	// return nil
	tx := m.Orm.Begin()
	err := tx.Model(&mode.Db_pm{}).Where("fullpath in ?", pmUrlList.Pm).Find(&exitesPm).Error
	if err != nil {
		return err
	}
	// var role mode.Db_roles
	// err = tx.Model(&mode.Db_roles{}).Where("id = ?", id.Id).Find(&role).Error
	// if err != nil {
	// 	return err
	// }
	// role.Perm = exitesPm
	// err = tx.Save(&role).Error
	role := &mode.Db_roles{
		Model: gorm.Model{ID: uint(id.Id)},
	}
	err = tx.Model(&role).Association("Perm").Replace(&exitesPm)
	if err != nil {
		return err
	}
	tx.Commit()
	return nil
}

/** 判断路径是否创建*/
func (m *PmDao) PmSqlExitsD(param *dto.Ipm_add) (*mode.Db_pm, error) {
	var exites mode.Db_pm
	err := m.Orm.Model(&mode.Db_pm{}).Where("url = ? and method = ?", param.Url, param.Method).Find(&exites).Error
	if err != nil {
		return nil, err
	}
	return &exites, nil
}

/** 获取path列表*/
func (m *PmDao) Pm_R_listD() (*[]mode.Db_pm, error) {
	var Pmlist []mode.Db_pm
	if err := m.Orm.Model(&mode.Db_pm{}).Find(&Pmlist).Error; err != nil {
		return nil, err
	}
	return &Pmlist, nil
}

/** 角色更新path列表*/
func (m *PmDao) PmAcRolesD(id *dto.ReqUriIdDto, param *dto.Ipm_roles) error {
	// 创建虚拟
	path := &mode.Db_roles{
		Model: gorm.Model{ID: uint(id.Id)},
	}
	var list []mode.Db_pm
	for _, v := range param.Roles {
		list = append(list, mode.Db_pm{Model: gorm.Model{ID: uint(v)}})
	}
	return m.Orm.Model(path).Association("Perm").Replace(&list)
}

func (m *PmDao) PmCreateD(ac *dto.Ipm_add) error {
	return m.Orm.Save(ac.CoverPm()).Error
}
func (m *PmDao) PmAddListD(ac *dto.Ipm_addList) error {
	return m.Orm.Model(&mode.Db_pm{}).Save(ac.Paths).Error
}

// func (m *PmDao) PmUserFindRole(user string) ([]string, error) {
// 	var exits []mode.CasbinRule
// 	var roleList []string
// 	err := m.Orm.Where("Ptype = 'g' and v0 = ?", user).Find(&exits).Error
// 	if err != nil {
// 		return nil, err
// 	}
// 	for _, v := range exits {
// 		roleList = append(roleList, v.V1)
// 	}
// 	return roleList, nil
// }
